<?php

namespace app\common\model\stock;

use app\common\model\goods\GoodsHouseStock;
use think\Db;
use think\Model;


class Stock extends Model
{

    // 表名
    protected $name = 'stock_report';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;


    public function loss_status(){
        return array(
            1=>'正在进行',
            2=>'已完成',
            3=>'驳回',
        );
    }
    public function addstocks(){
        $report_id = input('report_id/d',0);
        $goods_id = input('goods_id/d',0);
        $stock = input('stock/d',0);

        $rrow = Db::name('stock_report')->where(array('id'=>$report_id))->find();

        if(!$rrow || $rrow['status'] !=1){
            return array('code'=>0,'msg'=>'报表不存在,或者已经完结');
            $this->error('报表不存在');
        }
        $group = Db::name('auth_group_access')->alias('a')->where(array('uid' => session('admin')['id']))->join('zax_auth_group g', 'a.group_id = g.id')->find();

        $rr =  Db::name('stock_report_goods')->where(array('report_id'=>$report_id,'goods_id'=>$goods_id))->find();
        if($rr){
            $data = array(
                'stock'=>$stock,
                'createtime'=>time(),
                'admin_id'=>session('admin')['id'],
                'group_id'=>$group['group_id'],
                'company'=>$group['name'],
                'operator'=>session('admin')['nickname'],
            );
            $res = Db::name('stock_report_goods')->where(['id'=>$rr['id']])->update($data);
            if($res){
                return array('code'=>1,'msg'=>'添加成功');
//                $this->success('添加成功');
            }else{
                return array('code'=>0,'msg'=>'添加失败');
                $this->success('添加失败');
            }
        }


        $where = array(
            'storehouse_id'=>$rrow['storehouse_id'],
            'goods_id'=>$goods_id
        );
        $srow = Db::name('goods_house_stock')->where($where)->find();


        $data = array(
            'report_id'=>$report_id,
            'goods_id'=>$goods_id,
            'stock'=>$stock,
            'old_stock'=>empty($srow['stock'])?0:$srow['stock'],
            'costPrice'=>empty($srow['stock'])?0:bcdiv($srow['balance'],$srow['stock'],4),
            'createtime'=>time(),
            'admin_id'=>session('admin')['id'],
            'group_id'=>$group['group_id'],
            'company'=>$group['name'],
            'operator'=>session('admin')['nickname'],
        );
        $res = Db::name('stock_report_goods')->insert($data);
        if($res){
            return array('code'=>1,'msg'=>'添加成功');
            $this->success('添加成功');
        }else{
            return array('code'=>0,'msg'=>'添加失败');
            $this->success('添加失败');
        }
    }
    public function storehouse()
    {
        return $this->belongsTo('app\common\model\Storehouse', 'storehouse_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
